package myJs.api

import org.scalajs.dom.window
import upickle.default._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo.{UserData, UserNameData, UserRowData, ValidData}
import shared.pojo.User._
import sttp.client3._
import sttp.model._

import scala.concurrent.Future

/**
 * Created by yz on 7/11/2022
 */
object UserApi {

  val baseUrl = s"${Api.apiUrl}/liveForest/user"


  def getLimit(request: UserNameData) = {
    val url = s"${baseUrl}/getLimit"
    Api.preparseRequest[Boolean]
      .post(uri"${url}")
      .body(write(request))
      .send(Api.sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def changePassword(request: ChangePassword.Request) = {
    val url = s"${baseUrl}/changePassword"
    Api.preparseRequest[ValidData]
      .post(uri"${url}")
      .body(write(request))
      .send(Api.sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def detailInfo(request:UserIdData) = {
    val url = s"${baseUrl}/detailInfo"
    Api.preparseRequest[UserRowData]
      .post(uri"${url}")
      .body(write(request))
      .send(Api.sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }

  def getPdfInfo(request: UserIdData) = {
    val url = s"${baseUrl}/getPdfInfo"
    Api.preparseRequest[PdfInfo]
      .post(uri"${url}")
      .body(write(request))
      .send(Api.sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }
  def updatePdfInfo(request: PdfInfo) = {
    val url = s"${baseUrl}/updatePdfInfo"
    Api.preparseRequest[ValidData]
      .post(uri"${url}")
      .body(write(request))
      .send(Api.sttpBackend)
      .map(_.body)
      .flatMap(Future.fromTry)
  }




}
